<template>
  <!-- 速度设置 -->
  <div class="app-container">
    <!-- 搜索查询 -->
    <div class="form-container">
      <el-form ref="queryForm" :inline="true" label-width="80px" :model="filters">
        <el-form-item label="地貌类型">
          <el-select v-model="filters.regionTypeId" clearable size="medium" placeholder="请选择地貌类型">
            <el-option
              v-for="item in regionOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <!--操作按钮-->
    <div class="btns-group">
      <superviseBtns :single="single" has-permi="workloadConfig" />
      <span class="personal-type">
        <el-button icon="el-icon-arrow-left" size="mini" @click="handleReturn()">返回</el-button>
      </span>
    </div>
    <!-- 表格展示start -->
    <el-table
      ref="multipleTable"
      v-loading="loading"
      class="table-list"
      stripe
      :class="highLevelSet===true ? (needTagsView===true ? 'el-body-page-roll-tag-height' : 'el-body-page-roll-height') :
        (needTagsView===true ? 'el-body-page-roll-tag' : 'el-body-page-roll')"
      :data="tableData"
      :row-style="rowClass"
      @selection-change="handleSelectionChange"
      @row-click="rowChecked"
    >
      <el-table-column type="selection" width="38" />
      <el-table-column type="index" label="序号" width="51" align="center" />
      <el-table-column prop="regionTypeId" :formatter="formartType" label="地貌类型" min-width="180" align="center" />
      <el-table-column prop="maxWorkload" label="最大工作量" min-width="180" align="center" />
      <el-table-column prop="createTime" label="创建时间" min-width="120" align="center" />
    </el-table>
    <!-- 表格展示end-->

    <!-- 分页器 -->
    <pagination
      class="pagination-container"
      :total="total"
      :page.sync="filters.pageNum"
      :limit.sync="filters.pageSize"
      @pagination="getList"
    />
    <!-- 工作量新增 -->
    <addDialog ref="addDialog" :region-options="regionOptions" />
    <!-- 工作量详情 -->
    <detailDialog ref="detailDialog" :region-options="regionOptions" />
  </div>
</template>

<script>
import { listRegionalType } from '@/api/superviseBaseInfo/regionalSettings'
import { listWorkLoad, removeWorkLoad } from '@/api/superviseBaseInfo/typeConfig/workload'
// 按钮组件
import superviseBtns from '@/components/Common/Btns/superviseBtns'
// 新增弹框
import addDialog from './components/addDialog'
// 详情弹框
import detailDialog from './components/detailDialog'
import { mapState } from 'vuex'
export default {
  name: 'Workload',
  components: {
    addDialog,
    detailDialog,
    superviseBtns
  },
  data() {
    return {
      // 筛选条件
      highLevelSet: false, // 高级设置显示隐藏
      filters: {
        pageNum: 1,
        pageSize: 20,
        regionTypeId: '', // 地貌类型
        equipmentId: ''
      },
      /* 列表 start*/
      total: 0, // 分页总条数
      single: true, // 非单个禁用
      selectRow: [], // 高亮样式
      multipleSelection: [], // 选中表格行
      regionOptions: [], // 地区类型
      loading: false, // 列表加载
      tableData: []
      /* 列表 end*/
    }
  },
  computed: {
    ...mapState({
      needTagsView: (state) => state.settings.tagsView
    }),
    // 高级搜索显示隐藏
    handleTagShow() {
      return this.highLevelSet
    }
  },
  created() {
    const { id } = this.$route.query
    this.filters.equipmentId = id
    // 获取列表数据
    this.getList()
    this.listRegionalType()
  },
  methods: {
    // 返回
    handleReturn() {
      this.$router.go(-1)
    },
    async listRegionalType() {
      const { data: { rows }} = await listRegionalType()
      if (rows) {
        this.regionOptions = rows.map(item => ({ label: item.typeName, value: item.id }))
        this.regionOptions.unshift({ label: '全部', value: '' })
      }
    },
    formartType(row) {
      if (JSON.stringify(row) === '{}') return
      const item = this.regionOptions.find(item => item.value === row.regionTypeId)
      return item && item.label
    },
    // 获取列表数据
    async getList() {
      this.loading = true
      try {
        const { data: { rows, total }} = await listWorkLoad(this.filters)
        this.total = total
        this.tableData = rows
        // this.addTaleData()
        this.loading = false
      } catch {
        this.loading = false
      }
    },
    // list表格填充空白行
    addTaleData() {
      if (this.tableData.length < this.fristPageSize) {
        const arr = {}
        this.tableData.push(arr)
        this.addTaleData()
      }
    },
    /* 表格选中行事件 start */
    rowChecked(row) {
      const selectData = this.multipleSelection
      this.$refs.multipleTable.clearSelection()
      if (selectData.length === 1) {
        selectData.forEach((item) => {
          // 判断 如果当前的一行被勾选, 再次点击的时候就会取消选中
          if (item === row) {
            this.$refs.multipleTable.toggleRowSelection(row, false)
          } else {
            this.$refs.multipleTable.toggleRowSelection(row, true)
          }
        })
      } else {
        this.$refs.multipleTable.toggleRowSelection(row, true)
      }
    },
    // 点击表格前面选框时触发的事件
    handleSelectionChange(val) {
      if (JSON.stringify(val[0]) === '{}') {
        this.$refs.multipleTable.clearSelection()
        val = []
      }
      if (val.length === 1) {
        this.single = false
      } else {
        this.single = true
      }
      this.selectRow = []
      this.multipleSelection = val
      if (this.multipleSelection.length > 0) {
        this.multipleSelection.forEach((item, index) => {
          this.selectRow.push(this.tableData.indexOf(item))
        })
      }
      if (val.length > 1) {
        this.$refs.multipleTable.clearSelection()
        this.$refs.multipleTable.toggleRowSelection(val.pop())
      }
    },
    // 高亮样式
    rowClass({ row, rowIndex }) {
      if (this.selectRow.includes(rowIndex)) {
        return { 'background-color': 'rgba(185,221,249,.75)' }
      }
    },
    /* 表格选中行事件 end */

    /* 按钮 Start */
    // 搜索方法
    handleQuery() {
      this.filters.pageNum = 1
      this.getList()
    },
    // 重置搜索
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 新增
    handleAdd() {
      this.$refs.addDialog.handleAdd('新增速度设置')
    },
    // 编辑
    handleUpdate() {
      this.$refs.addDialog.handleAdd('修改速度设置', this.multipleSelection[0].id)
    },
    // 详情
    handleDetail() {
      this.$refs.detailDialog.handleAdd('速度设置详情', this.multipleSelection[0].id)
    },
    // 删除
    handleDelete() {
      this.$confirm('确认要删除此工作量吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          this.loading = true
          // 通过id删除
          await removeWorkLoad(this.multipleSelection[0].id)
          this.loading = false
          this.msgSuccess('删除成功！')
          // 重新加载表格数据
          this.getList()
        })
        .catch(() => {
          this.msgWarning('已取消删除')
        })
    }
    /* 按钮 end */
  }
}
</script>

<style scoped lang="scss">
</style>

